//
//  OwnerPropertyTypeView.swift
//  ezyform
//
//  Created by Moennig Q on 3/9/2022.
//  Copyright © 2022 Client.Solution Pty Ltd. All rights reserved.
//

import UIKit
import SnapKit
import RxCocoa
import RxSwift
enum PropertyType: String{//value 和name是一样的
    case residential = "Residential"
    case school = "School"
    case commercial  = "Commercial"
}
class OwnerPropertyTypeView: View {
    private lazy var disposeBag = DisposeBag()
    lazy var requiredLabel: BaseLabel = {
        return ViewFactory.shared.generateRequiredLabel()
    }()
    var currentPropertyType: PropertyType = .residential
    let residentialBtn = UIButton()
    let schoolBtn = UIButton()
    let commertialBtn = UIButton()
    var selectPropertyTypeClosure: ((PropertyType)->Void)?
    
    override func setUpView() {
        self.backgroundColor = UIColor(hexStr: Constant.TransparentColorHexStr)
        //
        let btnOriginalSize = CGSize(width: 110, height: 75)
        var realSize = btnOriginalSize
        
        if APPConfig.WidthForPortrait < (btnOriginalSize.width * 3 + Constant.LeftRightMargin * 2 + Constant.InternalMargin * 2) {//大屏
            realSize.width = (APPConfig.WidthForPortrait - Constant.LeftRightMargin * 4) / 3
            realSize.height = realSize.width / btnOriginalSize.width * btnOriginalSize.height
        }
        
        //title
        let propertyTypeLabel = LabelWithRedStarView(R.string.localizable.propertyType())
        self.addSubview(propertyTypeLabel)
        propertyTypeLabel.snp.makeConstraints { make in
            make.left.top.equalToSuperview()
            make.height.width.greaterThanOrEqualTo(0)
        }
        //residential
        self.addSubview(residentialBtn)
        residentialBtn.snp.makeConstraints { make in
            make.top.equalTo(propertyTypeLabel.snp.bottom).offset(Constant.TitleTextMargin)
            make.left.equalTo(propertyTypeLabel)
            make.size.equalTo(realSize)
            make.bottom.equalToSuperview()
        }
        //school
        self.addSubview(schoolBtn)
        schoolBtn.snp.makeConstraints { make in
            make.top.equalTo(residentialBtn)
            make.centerX.equalToSuperview()
            make.size.equalTo(realSize)
        }
        //commertial
        self.addSubview(commertialBtn)
        commertialBtn.snp.makeConstraints { make in
            make.right.equalToSuperview()
            make.top.equalTo(residentialBtn)
            make.size.equalTo(realSize)
        }
        //监听tap事件
        residentialBtn.rx.tap.subscribe { [weak self] _ in
            guard let strongSelf = self else {return}
            if strongSelf.currentPropertyType !=  .residential {
                strongSelf.setPropertyType(.residential)
                strongSelf.selectPropertyTypeClosure?(strongSelf.currentPropertyType)
            }
        }.disposed(by: self.disposeBag)
        //监听tap事件
        schoolBtn.rx.tap.subscribe { [weak self] _ in
            guard let strongSelf = self else {return}
            if strongSelf.currentPropertyType !=  .school {
                strongSelf.setPropertyType(.school)
                strongSelf.selectPropertyTypeClosure?(strongSelf.currentPropertyType)
            }
        }.disposed(by: self.disposeBag)
        //监听tap事件
        commertialBtn.rx.tap.subscribe { [weak self] _ in
            guard let strongSelf = self else {return}
            if strongSelf.currentPropertyType !=  .commercial {
                strongSelf.setPropertyType(.commercial)
                strongSelf.selectPropertyTypeClosure?(strongSelf.currentPropertyType)
            }
        }.disposed(by: self.disposeBag)
    }
    func setPropertyType(_ type: PropertyType){
        self.currentPropertyType = type
        self.residentialBtn.setImage(UIImage(named: "PVDDetal_residential_type_unsel"), for: UIControl.State.normal)
        self.schoolBtn.setImage(UIImage(named: "PVDDetal_school_type_unsel"), for: UIControl.State.normal)
        self.commertialBtn.setImage(UIImage(named: "PVDDetal_commercial_type_unsel"), for: UIControl.State.normal)
        switch type {
        case .residential:
            self.residentialBtn.setImage(UIImage(named: "PVDDetal_residential_type"), for: UIControl.State.normal)
        case .school:
            self.schoolBtn.setImage(UIImage(named: "PVDDetal_school_type"), for: UIControl.State.normal)
        case .commercial:
            self.commertialBtn.setImage(UIImage(named: "PVDDetal_commercial_type"), for: UIControl.State.normal)
        }
    }
}
